import { getKeyRouter } from '@/utils';
import { message } from 'antd';
import { history } from 'umi';

const parsePurchaseString = (inputString) => {
  let PurchaseTableName = [
    'asset_storage',
    'asset_apply_stuffasset',
    'asset_apply_allot',
    'asset_inventory_difference',
    'asset_inventory',
    'asset_mainten',
    'asset_apply_handle',
    'asset_apply_storage',
    'asset_apply_transfer',
    'asset_apply_ues',
    'asset_apply_return', //这个需要放最后，不然有source在中间的就会先用这个
    'asset_apply_change',
  ];
  // 直接将接收的字符串作为id
  let id = inputString;
  // 提取字符串部分，截取到purchase_source
  let key = PurchaseTableName.find((item) => inputString.startsWith(item));
  // 如果没有找到匹配项，返回false
  if (!key) {
    message.warning('返回businessID不匹配，请进行检查！');
    return false;
  }
  // 返回结果
  return {
    id: id,
    key: key,
  };
};

/**
 *
 * @param payload 参数列表
 * @param homePageRouterText 参数列表不存在时跳转当前页，location.pathname
 *
 */
export const jumpRoutes = (payload: Record<string, any> | any, homePageRouterText: any) => {
  if (payload && payload?.id) {
    if (payload?.businessID && parsePurchaseString(payload?.businessID)) {
      let resObj = parsePurchaseString(payload?.businessID);
      if (resObj) {
        const obj = {
          id: payload.businessID,
          orderType: payload.orderType,
          systemType: payload.systemType,
          taskID: payload.taskID,
        };
        let { id, key } = resObj;
        if (key == 'asset_storage') {
          // 入库登记详情
          history.push({
            pathname: `/assetStorage/details/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_inventory_difference') {
          // 盘点汇报详情
          history.push({
            pathname: `/assetInventoryDifference/details/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_inventory') {
          // 盘点计划详情
          history.push({
            pathname: `/assetInventory/details/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'view',
              id,
              pageInfo: { id },
              taskId: payload?.id,
              pIDDetailInfo: { id },
            },
          });
        } else if (key == 'asset_apply_stuffasset') {
          // 材料转资产
          history.push({
            pathname: `/assetApplyStuffasset/detail/{id}/{applyType}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_apply_handle') {
          //  资产报废
          history.push({
            pathname: `/assetApplyHandle/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_apply_storage') {
          // 到货验收
          history.push({
            pathname: `/assetApplyStorage/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_apply_transfer') {
          // 资产调拨
          history.push({
            pathname: `/assetApplyTransfer/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_apply_allot') {
          // 资产交接
          history.push({
            pathname: `/assetApplyAllot/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_apply_ues') {
          // 领用
          history.push({
            pathname: `/assetApplyUes/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: { ...obj, queryType: '1' },
            },
          });
        } else if (key == 'asset_apply_return') {
          // 退库
          history.push({
            pathname: `/assetApplyReturn/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: { ...obj, queryType: '2' },
            },
          });
        } else if (key == 'asset_mainten') {
          // 维保
          history.push({
            pathname: `/assetMainten/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        } else if (key == 'asset_apply_change') {
          // 资产信息变更
          history.push({
            pathname: `/assetApplyChange/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'detail',
              record: obj,
            },
          });
        }
      }
    }
  } else {
    history.push(homePageRouterText); //去管理界面
  }
};
